package cn.itcast.up.common.mytest

import com.typesafe.config.{Config, ConfigFactory}
import pureconfig.ConfigSource
import pureconfig.error.ConfigReaderFailures


/**
 * up.conf配置文件的加载类.
 * application.conf
 * ConfigFactory.load()
 */
case class UpHolder(
                     model: Model,
                     hadoop: Hadoop,
                     mysql: MySQL,
                     oozie: Oozie
                   )

// 定义Model,封装model中的数据
case class Model(
                  user: String,
                  app: String,
                  path: Path
                )

// 定义一个Path,封装path路径
case class Path(
                 jars: String,
                 modelBase: String
               )

case class Hadoop(
                   nameNode: String,
                   resourceManager: String
                 )

case class MySQL(
                  url: String,
                  driver: String,
                  tagTable: String,
                  modelTable: String
                )

case class Oozie(
                  url: String,
                  //我们用Map将所有的kv键值对都封装到一起.
                  params: Map[String, String]
                )

//定义一个伴生对象,将数据提供出去.
object UpHolder {


  //将up.conf配置文件中的数据,装载到UpHolder对象中

  //1. 加载up.conf配置文件
  //ConfigFactory.load()不传参数,默认找application.conf配置文件,如果传递参数,只需要给文件名
  private val config: Config = ConfigFactory.load("up")
  //2. 将获取到的config对象装载为UpHolder对象中
  //  private val left: Either.LeftProjection[ConfigReaderFailures, UpHolder] = ConfigSource.fromConfig(config).load[UpHolder].left
  private val upholder: UpHolder = ConfigSource.fromConfig(config)
    //开始装载数据
    .load[UpHolder]
    //获取右边的数据
    .right.get
  //3. 对外提供数据,将model,mysql,oozie等配置对象暴露出去
  val model: Model = upholder.model
  val hadoop: Hadoop = upholder.hadoop
  val mysql: MySQL = upholder.mysql
  val oozie: Oozie = upholder.oozie

  def main(args: Array[String]): Unit = {
    println(model)
    println(hadoop)
    println(mysql)
    println(oozie)
  }
}









